<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/08/18
 * Time: 10:15
 * Description：广渠路店活动
 */

class GuangQuLuController extends BaseController {

    /**
     * @Author:      sqq
     * @DateTime:    2018-08-18 10:16:30
     * @Description: 广渠路店新店开业活动-登录领取优惠券 [洗车活动]
     * @return []
     * @link： http://www.c_activity.com/v1/activity/guangqulu/login?phone=15910959937&check_code=2814&register_source_id=10&source=1
     */
    public function login(){
        $validator = Validator::make(Input::all(),
            array(
                "phone" => "required",
                "check_code" => "required",
                'register_source_id' => 'required',
                'source' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $area = '010';
            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);
            if (is_null($record)) { //没有记录
                $response = Lang::get("error.user_login_checkcode_null_10004");
                break;
            }
            //校验是否超过时限
            if (time() > Common::datetime2Num($record->out_time_at)) {
                $response = Lang::get("error.user_login_checkcode_timeout_10005");
                break;
            }
            //校验验证码是否正确
            if ($record->check_code != Input::get('check_code')) {
                $response = Lang::get("error.user_login_checkcode_error_10006");
                break;
            }

            $wechat = Users::getUser(Input::get('phone'));
            if (is_null($wechat)) {
                try{
                    $wechat = new Users();
                    $wechat->phone = Input::get('phone');
                    $wechat->status = 1;
                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店
                    $wechat->real_name = '';
                    $wechat->sex = 0;
                    $wechat->area = '010';
                    $wechat->type = 1;
                    $wechat->activate = 1;
                    $wechat->nickname = '';
                    $wechat->heading_img = '';
                    $wechat->member = Config::get('user.def_member');
                    $wechat->save();
                    //新用户注册用券
                    if(Config::get("activity.register_coupons") == 1){
                        $activity = Coupons::getNewUserRegister(Config::get('activity.activity_id'),$response);
                        if(!is_null($activity)){
                            foreach ($activity as  $v) {
                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                                if (!$coupon_base_info) {
                                    continue;
                                }
                                if ($coupon_base_info->status == 0) {
                                    continue;
                                }
                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                    continue;
                                }
                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                                if (!is_null($coupon_whether_exist)) {
                                    $coupons = Coupons::find($coupon_whether_exist->id);
                                    $coupons->status = 2;
                                    $coupons->received_user_id = $wechat->id;
                                    $coupons->get_time = date("Y-m-d H:i:s", time());
                                    if ($coupon_base_info->date_type == 1) { //顺延日期
                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                    }
                                    $coupons->save();
                                }
                            }
                        }
                    }
                }catch (Exception $e){
                    Log::error("error", array("system" => "activity" ,"event" => "/GuangQuLu/login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("error.login_create_token_error_21005");
                    break;
                }
            }

            if($wechat->status == 2){
                $response = Lang::get('error.user_frozen_error_21002');
                break;
            }

            //如果发现此用户是老用户，且未激活 直接激活
            if($wechat->type == 1 && $wechat->activate == 0){

                try{
                    //记录是否是新老用户
                    $is_new = NewOldUserGuangQuLu::where("user_id",$wechat->id)->first();
                    if(is_null($is_new)){
                        $new_user = new NewOldUserGuangQuLu();
                        $new_user->user_id = $wechat->id;
                        $new_user->phone = $wechat->phone;
                        $new_user->new = '新用户';
                        $new_user->source = Input::get('source',0);
                        $new_user->created_at = date("Y-m-d H:i:s",time());
                        $new_user->updated_at = date("Y-m-d H:i:s",time());
                        $new_user->save();
                    }
                }catch (Exception $e){
                    Log::error("error", array("system" => "api3.0" ,"event" => "login/new_old_user_add", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("error.new_old_user_add_error_110001");
                    break;
                }

                //注册送券
                if(Config::get("activity.register_coupons") == 1){
                    $activity = Coupons::getNewUserRegister(Config::get('activity.activity_id'),$response);
                    if(!is_null($activity)){
                        foreach ($activity as  $v) {
                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                            if (!$coupon_base_info) {
                                continue;
                            }
                            if ($coupon_base_info->status == 0) {
                                continue;
                            }
                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                continue;
                            }
                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                            if (!is_null($coupon_whether_exist)) {
                                $coupons = Coupons::find($coupon_whether_exist->id);
                                $coupons->status = 2;
                                $coupons->received_user_id = $wechat->id;
                                $coupons->get_time = date("Y-m-d H:i:s", time());
                                if ($coupon_base_info->date_type == 1) { //顺延日期
                                    $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                }
                                $coupons->save();
                            }
                        }
                    }
                }
                $wechat->activate = 1;
                $wechat->save();
            }else {
                try{
                    //记录是否是新老用户
                    $is_new = NewOldUserGuangQuLu::where("user_id",$wechat->id)->first();
                    if(is_null($is_new)){
                        $new_user = new NewOldUserGuangQuLu();
                        $new_user->user_id = $wechat->id;
                        $new_user->phone = $wechat->phone;
                        $new_user->new = '老用户';
                        $new_user->source = Input::get('source',0);
                        $new_user->created_at = date("Y-m-d H:i:s",time());
                        $new_user->updated_at = date("Y-m-d H:i:s",time());
                        $new_user->save();
                    }
                }catch (Exception $e){
                    Log::error("error", array("system" => "api3.0" ,"event" => "login/new_old_user_add", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("error.new_old_user_add_error_110001");
                    break;
                }
            }

            try{
                $token="";
                $find_token = UserTokens::where('user_id',$wechat->id)->first();
                if(!is_null($find_token)){ //有token 判断token是否过期
                    if(strtotime($find_token->out_time_at) < time()){//说明过期了
                        UserTokens::where('user_id',$wechat->id)->delete();
                        $user_token = new UserTokens;
                        $user_token->user_id = $wechat->id;
                        $user_token->token = md5(Input::get('phone').time().rand(0,100));
                        $user_token->created_at = Common::num2DateTime(time());
                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                        $user_token->save();
                        $token=$user_token->token;
                    }else{
                        $token=$find_token->token;
                    }
                }else{
                    $user_token = new UserTokens;
                    $user_token->user_id = $wechat->id;
                    $user_token->token = md5(Input::get('phone').time().rand(0,100));
                    $user_token->created_at = Common::num2DateTime(time());
                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                    $user_token->save();
                    $token=$user_token->token;
                }

            } catch (Exception $e){
                Log::error("error", array("system" => "activity" ,"event" => "/GuangQuLu/login", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("error.login_create_token_error_21005");
                break;
            }


            //根据活动查到活动有效期
            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('activity.guangqulu_new_wash_activity_id'));
            if(!$activity_dateline){
                $response = Lang::get('error.activity_not_exist');
                break;
            }

            if(Config::get("activity.guangqulu_new_wash_open") == 1){
                $coupon_res = Coupons::getNewUserRegister(Config::get('activity.guangqulu_new_wash_activity_id'),$response);
                if(is_null($coupon_res)){
                    break;
                }

                $_coupon = array();
                $_coupon_list = array();

                //获取当前时间
                $now_time = date("Y-m-d",time());

                foreach ($coupon_res as  $_c) {
                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);

                    if(!$coupon_base_info){
                        $response = Lang::get('error.coupon_not_exit_error_51004');
                        break;
                    }
                    if($coupon_base_info->status == 0){
                        // $response = Lang::get('error.coupon_status_error_51005');
                        // break;
                        continue;
                    }
                    // if($coupon_base_info->date_type == 0){ //起止时间
                    //     if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){
                    //         $response = Lang::get('error.coupon_game_over_error_51006');
                    //         break;
                    //     }
                    // }
                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);
                    if($coupons){
                        $response = Lang::get('error.coupon_received_error_51007');
                        break;
                    }

                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);
                    if(is_null($coupon_has_been_brought_out)){
                        $response = Lang::get("error.coupon_has_been_brought_out_51008");
                        break;
                    }
                    $end_date = null;
                    if($coupon_base_info->date_type == 1){ //顺延日期
                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));
                    }

//                    if($coupon_base_info->begin_date == $now_time && $coupon_base_info->end_date == $now_time){
//                        $status = 2;
//                    }else{
//                        $status = 6;
//                    }
                    $status = 2;
                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = $status,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' and received_user_id = '0' limit 1", array(
                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id
                    ));
                    if (!$status) {
                        $response = Lang::get("error.coupon_receive_not_success_error_51009");
                        break;
                    }
                    if($coupon_base_info->date_type == 1){
                        $begin_date = date('Y-m-d',time());
                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));
                    }else{
                        $begin_date = $coupon_base_info->begin_date;
                        $end_date  = $coupon_base_info->end_date;
                    }
                    $_coupon['begin_date'] = $begin_date;
                    $_coupon['end_date'] = $end_date;
                    $_coupon['name'] = $coupon_base_info->name;
                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;
                    $_coupon['amount'] = $coupon_base_info->amount;
                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];
                    $_coupon['limit_order'] = $coupon_base_info->limit_order;
                    array_push($_coupon_list, $_coupon);

                }
                $response["result"] = array(
                    "coupon_info" => $_coupon_list,
                );
            }
            $response["result"] = array(
                "user_info" => array(
                    'id'=> $wechat->id,
                    'token' => $token,
                    'phone' => $wechat->phone,
                    'real_name' => $wechat->real_name,
                    'sex' => $wechat->sex,
                    'area' => $wechat->area,
                    'remark' => $wechat->remark,
                    'type' => $wechat->type,
                    'status' =>$wechat->status,
                    'nickname' => $wechat->nickname,
                    'birthday' => $wechat->birthday,
                    'heading_img' =>  $wechat->heading_img,
                    'heading_img_url' =>  Config::get('config.heading_img_url'),
                ),
            );

        }while(false);
        Log::info("info", array("system" => "activity" ,"event" => "/GuangQuLu/login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

   /*
    * @Author:      sqq
    * @DateTime:    2018-08-14 12:10:32
    * @Description: 广渠路店新店开业活动(洗车活动)统计分享次数
    * @link: http://www.c_activity.com/v1/activity/guangqulu/share_num
    * */
    public function share_num(){

        do {
            $sql = CarGuangQuLuWash::first();
            if(!is_null($sql)){
                $sql->num += 1;
                $sql->updated_at = date("Y-m-d H:i:s",time());
            }else{
                $sql = new CarGuangQuLuWash();
                $sql->num = 1;
                $sql->created_at = date("Y-m-d H:i:s",time());
                $sql->updated_at = date("Y-m-d H:i:s",time());
            }
            if($sql->save()){
                $response = array('code' => 0, 'msg' => 'ok');
            }else{
                $response = array('code' => 1000, 'msg' => '操作失败');
            }

        } while (false);
        Log::info("info", array("system" => "activity" ,"event" => "/GuangQuLu/share_num", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);

    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-18 10:54:30
     * @Description: 广渠路店新店开业活动-登录领取优惠券 [保养完成后领机油券]
     * @return []
     * @link：
     */
    public function maintain_login(){
        $validator = Validator::make(Input::all(),
            array(
                "phone" => "required",
                "check_code" => "required",
                'register_source_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $area = '010';
            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);
            if (is_null($record)) { //没有记录
                $response = Lang::get("error.user_login_checkcode_null_10004");
                break;
            }
            //校验是否超过时限
            if (time() > Common::datetime2Num($record->out_time_at)) {
                $response = Lang::get("error.user_login_checkcode_timeout_10005");
                break;
            }
            //校验验证码是否正确
            if ($record->check_code != Input::get('check_code')) {
                $response = Lang::get("error.user_login_checkcode_error_10006");
                break;
            }

            $wechat = Users::getUser(Input::get('phone'));
            if (is_null($wechat)) {
                try{
                    $wechat = new Users();
                    $wechat->phone = Input::get('phone');
                    $wechat->status = 1;
                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店
                    $wechat->real_name = '';
                    $wechat->sex = 0;
                    $wechat->area = '010';
                    $wechat->type = 1;
                    $wechat->activate = 1;
                    $wechat->nickname = '';
                    $wechat->heading_img = '';
                    $wechat->member = Config::get('user.def_member');
                    $wechat->save();
                    //新用户注册用券
                    if(Config::get("activity.register_coupons") == 1){
                        $activity = Coupons::getNewUserRegister(Config::get('activity.activity_id'),$response);
                        if(!is_null($activity)){
                            foreach ($activity as  $v) {
                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                                if (!$coupon_base_info) {
                                    continue;
                                }
                                if ($coupon_base_info->status == 0) {
                                    continue;
                                }
                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                    continue;
                                }
                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                                if (!is_null($coupon_whether_exist)) {
                                    $coupons = Coupons::find($coupon_whether_exist->id);
                                    $coupons->status = 2;
                                    $coupons->received_user_id = $wechat->id;
                                    $coupons->get_time = date("Y-m-d H:i:s", time());
                                    if ($coupon_base_info->date_type == 1) { //顺延日期
                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                    }
                                    $coupons->save();
                                }
                            }
                        }
                    }
                }catch (Exception $e){
                    Log::error("error", array("system" => "activity" ,"event" => "/maintain_login/login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("error.login_create_token_error_21005");
                    break;
                }
            }

            if($wechat->status == 2){
                $response = Lang::get('error.user_frozen_error_21002');
                break;
            }

            //如果发现此用户是老用户，且未激活 直接激活
            if($wechat->type == 1 && $wechat->activate == 0){
                //注册送券
                if(Config::get("activity.register_coupons") == 1){
                    $activity = Coupons::getNewUserRegister(Config::get('activity.activity_id'),$response);
                    if(!is_null($activity)){
                        foreach ($activity as  $v) {
                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                            if (!$coupon_base_info) {
                                continue;
                            }
                            if ($coupon_base_info->status == 0) {
                                continue;
                            }
                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                continue;
                            }
                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                            if (!is_null($coupon_whether_exist)) {
                                $coupons = Coupons::find($coupon_whether_exist->id);
                                $coupons->status = 2;
                                $coupons->received_user_id = $wechat->id;
                                $coupons->get_time = date("Y-m-d H:i:s", time());
                                if ($coupon_base_info->date_type == 1) { //顺延日期
                                    $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                }
                                $coupons->save();
                            }
                        }
                    }
                }
                $wechat->activate = 1;
                $wechat->save();
            }

            try{
                $token="";
                $find_token = UserTokens::where('user_id',$wechat->id)->first();
                if(!is_null($find_token)){ //有token 判断token是否过期
                    if(strtotime($find_token->out_time_at) < time()){//说明过期了
                        UserTokens::where('user_id',$wechat->id)->delete();
                        $user_token = new UserTokens;
                        $user_token->user_id = $wechat->id;
                        $user_token->token = md5(Input::get('phone').time().rand(0,100));
                        $user_token->created_at = Common::num2DateTime(time());
                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                        $user_token->save();
                        $token=$user_token->token;
                    }else{
                        $token=$find_token->token;
                    }
                }else{
                    $user_token = new UserTokens;
                    $user_token->user_id = $wechat->id;
                    $user_token->token = md5(Input::get('phone').time().rand(0,100));
                    $user_token->created_at = Common::num2DateTime(time());
                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                    $user_token->save();
                    $token=$user_token->token;
                }

            } catch (Exception $e){
                Log::error("error", array("system" => "activity" ,"event" => "/maintain_login/login", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("error.login_create_token_error_21005");
                break;
            }


            //根据活动查到活动有效期
            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('activity.guangqulu_new_maintain_activity_id'));
            if(!$activity_dateline){
                $response = Lang::get('error.activity_not_exist');
                break;
            }

            if(Config::get("activity.guangqulu_new_maintain_open") == 1){
                $coupon_res = Coupons::getNewUserRegister(Config::get('activity.guangqulu_new_maintain_activity_id'),$response);
                if(is_null($coupon_res)){
                    break;
                }

                $_coupon = array();
                $_coupon_list = array();

                //获取当前时间
                $now_time = date("Y-m-d",time());
                $shop_id = Config::get('activity.guangqulu_new_maintain_shop_id');
                $product_id = Config::get('activity.guangqulu_new_maintain_product_id');
                $order_status = array(1117,1217,1317,1417,1018);

                foreach ($coupon_res as  $_c) {
                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);

                    if(!$coupon_base_info){
                        $response = Lang::get('error.coupon_not_exit_error_51004');
                        break;
                    }
                    if($coupon_base_info->status == 0){
                        // $response = Lang::get('error.coupon_status_error_51005');
                        // break;
                        continue;
                    }
                    // if($coupon_base_info->date_type == 0){ //起止时间
                    //     if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){
                    //         $response = Lang::get('error.coupon_game_over_error_51006');
                    //         break;
                    //     }
                    // }
                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);
                    if($coupons){
                        $response = Lang::get('error.coupon_received_error_51007');
                        break;
                    }

                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);
                    if(is_null($coupon_has_been_brought_out)){
                        $response = Lang::get("error.coupon_has_been_brought_out_51008");
                        break;
                    }

                    //判断是否在当前门店下过单，并且包含服务包ID为 102、79、155、154、153中的任意一个
                    $orders = Orders::join('order_product','orders.order_id','=','order_product.order_id')
                        ->where('orders.user_id',$wechat->id)
                        ->where('orders.shop_id',$shop_id)
                        ->where('order_product.product_type','activity')
                        ->whereIn('order_product.product_id',$product_id)
                        ->where('order_product.status',1)
                        ->whereIn('orders.status',$order_status)
                        ->count();
                    if($orders > 0){
                        $end_date = null;
                        if($coupon_base_info->date_type == 1){ //顺延日期
                            $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));
                        }

//                    if($coupon_base_info->begin_date == $now_time && $coupon_base_info->end_date == $now_time){
//                        $status = 2;
//                    }else{
//                        $status = 6;
//                    }
                        $status = 2;
                        $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = $status,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' and received_user_id = '0' limit 1", array(
                            $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id
                        ));
                        if (!$status) {
                            $response = Lang::get("error.coupon_receive_not_success_error_51009");
                            break;
                        }
                        if($coupon_base_info->date_type == 1){
                            $begin_date = date('Y-m-d',time());
                            $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));
                        }else{
                            $begin_date = $coupon_base_info->begin_date;
                            $end_date  = $coupon_base_info->end_date;
                        }
                        $_coupon['begin_date'] = $begin_date;
                        $_coupon['end_date'] = $end_date;
                        $_coupon['name'] = $coupon_base_info->name;
                        $_coupon['limit_amount'] = $coupon_base_info->limit_amount;
                        $_coupon['amount'] = $coupon_base_info->amount;
                        $_coupon['type'] = Coupons::$type[$coupon_base_info->type];
                        $_coupon['limit_order'] = $coupon_base_info->limit_order;
                        array_push($_coupon_list, $_coupon);
                        $response["result"] = array(
                            "coupon_info" => $_coupon_list,
                        );
                    }else{
                        $response = Lang::get('error.coupon_activity_not_qualifications_error_100001');
                        break;
                    }
                }
            }
            $response["result"] = array(
                "user_info" => array(
                    'id'=> $wechat->id,
                    'token' => $token,
                    'phone' => $wechat->phone,
                    'real_name' => $wechat->real_name,
                    'sex' => $wechat->sex,
                    'area' => $wechat->area,
                    'remark' => $wechat->remark,
                    'type' => $wechat->type,
                    'status' =>$wechat->status,
                    'nickname' => $wechat->nickname,
                    'birthday' => $wechat->birthday,
                    'heading_img' =>  $wechat->heading_img,
                    'heading_img_url' =>  Config::get('config.heading_img_url'),
                ),
            );

        }while(false);
        Log::info("info", array("system" => "activity" ,"event" => "/maintain_login/login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-18 15:34:30
     * @Description: 广渠路店十元洗车活动
     * @return []
     * @link： http://www.c_activity.com/v1/activity/guangqulu/WashLogin?phone=15910959937&check_code=2814&register_source_id=10&source=1
     */
    public function WashLogin(){
        $validator = Validator::make(Input::all(),
            array(
                "phone" => "required",
                "check_code" => "required",
                'register_source_id' => 'required',
                'source' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $area = '010';
            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);
            if (is_null($record)) { //没有记录
                $response = Lang::get("error.user_login_checkcode_null_10004");
                break;
            }
            //校验是否超过时限
            if (time() > Common::datetime2Num($record->out_time_at)) {
                $response = Lang::get("error.user_login_checkcode_timeout_10005");
                break;
            }
            //校验验证码是否正确
            if ($record->check_code != Input::get('check_code')) {
                $response = Lang::get("error.user_login_checkcode_error_10006");
                break;
            }

            //校验用户类型
            $wechat_personal = Users::getTypeUser(Input::get('phone'),1);
            $wechat_enterprise = Users::getTypeUser(Input::get('phone'),2);
            if(is_null($wechat_personal) && !is_null($wechat_enterprise)){
                $response = Lang::get("activity_error.enterprise__not_exot_error_participation_1008");
                break;
            }
            $wechat = $wechat_personal;
            if (is_null($wechat_personal) && is_null($wechat_enterprise)) {
                try{
                    $wechat = new Users();
                    $wechat->phone = Input::get('phone');
                    $wechat->status = 1;
                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店
                    $wechat->real_name = '';
                    $wechat->sex = 0;
                    $wechat->area = '010';
                    $wechat->type = 1;
                    $wechat->activate = 1;
                    $wechat->nickname = '';
                    $wechat->heading_img = '';
                    $wechat->member = Config::get('user.def_member');
                    $wechat->save();
                    //新用户注册用券
                    if(Config::get("activity.register_coupons") == 1){
                        $activity = Coupons::getNewUserRegister(Config::get('activity.activity_id'),$response);
                        if(!is_null($activity)){
                            foreach ($activity as  $v) {
                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                                if (!$coupon_base_info) {
                                    continue;
                                }
                                if ($coupon_base_info->status == 0) {
                                    continue;
                                }
                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                    continue;
                                }
                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                                if (!is_null($coupon_whether_exist)) {
                                    $coupons = Coupons::find($coupon_whether_exist->id);
                                    $coupons->status = 2;
                                    $coupons->received_user_id = $wechat->id;
                                    $coupons->get_time = date("Y-m-d H:i:s", time());
                                    if ($coupon_base_info->date_type == 1) { //顺延日期
                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                    }
                                    $coupons->save();
                                }
                            }
                        }
                    }
                }catch (Exception $e){
                    Log::error("error", array("system" => "activity" ,"event" => "/GuangQuLu/login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("error.login_create_token_error_21005");
                    break;
                }
            }

            if($wechat->status == 2){
                $response = Lang::get('error.user_frozen_error_21002');
                break;
            }

            //如果发现此用户是老用户，且未激活 直接激活
            if($wechat->type == 1 && $wechat->activate == 0){

//                try{
//                    //记录是否是新老用户
//                    $is_new = NewOldUserGuangQuLu::where("user_id",$wechat->id)->first();
//                    if(is_null($is_new)){
//                        $new_user = new NewOldUserGuangQuLu();
//                        $new_user->user_id = $wechat->id;
//                        $new_user->phone = $wechat->phone;
//                        $new_user->new = '新用户';
//                        $new_user->source = Input::get('source',0);
//                        $new_user->created_at = date("Y-m-d H:i:s",time());
//                        $new_user->updated_at = date("Y-m-d H:i:s",time());
//                        $new_user->save();
//                    }
//                }catch (Exception $e){
//                    Log::error("error", array("system" => "api3.0" ,"event" => "login/new_old_user_add", 'input' =>Input::all() ,"exception" => $e));
//                    $response = Lang::get("error.new_old_user_add_error_110001");
//                    break;
//                }

                //注册送券
                if(Config::get("activity.register_coupons") == 1){
                    $activity = Coupons::getNewUserRegister(Config::get('activity.activity_id'),$response);
                    if(!is_null($activity)){
                        foreach ($activity as  $v) {
                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                            if (!$coupon_base_info) {
                                continue;
                            }
                            if ($coupon_base_info->status == 0) {
                                continue;
                            }
                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                continue;
                            }
                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                            if (!is_null($coupon_whether_exist)) {
                                $coupons = Coupons::find($coupon_whether_exist->id);
                                $coupons->status = 2;
                                $coupons->received_user_id = $wechat->id;
                                $coupons->get_time = date("Y-m-d H:i:s", time());
                                if ($coupon_base_info->date_type == 1) { //顺延日期
                                    $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                }
                                $coupons->save();
                            }
                        }
                    }
                }
                $wechat->activate = 1;
                $wechat->save();
            }
//            else {
//                try{
//                    //记录是否是新老用户
//                    $is_new = NewOldUserGuangQuLu::where("user_id",$wechat->id)->first();
//                    if(is_null($is_new)){
//                        $new_user = new NewOldUserGuangQuLu();
//                        $new_user->user_id = $wechat->id;
//                        $new_user->phone = $wechat->phone;
//                        $new_user->new = '老用户';
//                        $new_user->source = Input::get('source',0);
//                        $new_user->created_at = date("Y-m-d H:i:s",time());
//                        $new_user->updated_at = date("Y-m-d H:i:s",time());
//                        $new_user->save();
//                    }
//                }catch (Exception $e){
//                    Log::error("error", array("system" => "api3.0" ,"event" => "login/new_old_user_add", 'input' =>Input::all() ,"exception" => $e));
//                    $response = Lang::get("error.new_old_user_add_error_110001");
//                    break;
//                }
//            }

            try{
                $token="";
                $find_token = UserTokens::where('user_id',$wechat->id)->first();
                if(!is_null($find_token)){ //有token 判断token是否过期
                    if(strtotime($find_token->out_time_at) < time()){//说明过期了
                        UserTokens::where('user_id',$wechat->id)->delete();
                        $user_token = new UserTokens;
                        $user_token->user_id = $wechat->id;
                        $user_token->token = md5(Input::get('phone').time().rand(0,100));
                        $user_token->created_at = Common::num2DateTime(time());
                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                        $user_token->save();
                        $token=$user_token->token;
                    }else{
                        $token=$find_token->token;
                    }
                }else{
                    $user_token = new UserTokens;
                    $user_token->user_id = $wechat->id;
                    $user_token->token = md5(Input::get('phone').time().rand(0,100));
                    $user_token->created_at = Common::num2DateTime(time());
                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));
                    $user_token->save();
                    $token=$user_token->token;
                }

            } catch (Exception $e){
                Log::error("error", array("system" => "activity" ,"event" => "/GuangQuLu/login", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("error.login_create_token_error_21005");
                break;
            }


            //根据活动查到活动有效期
            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('activity.guangqulu_wash_ten_product_id'));
            if(!$activity_dateline){
                $response = Lang::get('error.activity_not_exist');
                break;
            }

            if(Config::get("activity.guangqulu_new_wash_ten_open") == 1){
                $coupon_res = Coupons::getNewUserRegister(Config::get('activity.guangqulu_wash_ten_product_id'),$response);
                if(is_null($coupon_res)){
                    break;
                }

                $_coupon = array();
                $_coupon_list = array();

                //获取当前时间
                $now_time = date("Y-m-d",time());

                foreach ($coupon_res as  $_c) {
                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);

                    if(!$coupon_base_info){
                        $response = Lang::get('error.coupon_not_exit_error_51004');
                        break;
                    }
                    if($coupon_base_info->status == 0){
                        // $response = Lang::get('error.coupon_status_error_51005');
                        // break;
                        continue;
                    }
                    // if($coupon_base_info->date_type == 0){ //起止时间
                    //     if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){
                    //         $response = Lang::get('error.coupon_game_over_error_51006');
                    //         break;
                    //     }
                    // }
                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);
                    if($coupons){
                        $response = Lang::get('error.coupon_received_error_51007');
                        break;
                    }

                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);
                    if(is_null($coupon_has_been_brought_out)){
                        $response = Lang::get("error.coupon_has_been_brought_out_51008");
                        break;
                    }
                    $end_date = null;
                    if($coupon_base_info->date_type == 1){ //顺延日期
                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));
                    }

//                    if($coupon_base_info->begin_date == $now_time && $coupon_base_info->end_date == $now_time){
//                        $status = 2;
//                    }else{
//                        $status = 6;
//                    }
                    $status = 2;
                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = $status,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' and received_user_id = '0' limit 1", array(
                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id
                    ));
                    if (!$status) {
                        $response = Lang::get("error.coupon_receive_not_success_error_51009");
                        break;
                    }
                    if($coupon_base_info->date_type == 1){
                        $begin_date = date('Y-m-d',time());
                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));
                    }else{
                        $begin_date = $coupon_base_info->begin_date;
                        $end_date  = $coupon_base_info->end_date;
                    }
                    $_coupon['begin_date'] = $begin_date;
                    $_coupon['end_date'] = $end_date;
                    $_coupon['name'] = $coupon_base_info->name;
                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;
                    $_coupon['amount'] = $coupon_base_info->amount;
                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];
                    $_coupon['limit_order'] = $coupon_base_info->limit_order;
                    array_push($_coupon_list, $_coupon);

                }
                $response["result"] = array(
                    "coupon_info" => $_coupon_list,
                );
            }
            $response["result"] = array(
                "user_info" => array(
                    'id'=> $wechat->id,
                    'token' => $token,
                    'phone' => $wechat->phone,
                    'real_name' => $wechat->real_name,
                    'sex' => $wechat->sex,
                    'area' => $wechat->area,
                    'remark' => $wechat->remark,
                    'type' => $wechat->type,
                    'status' =>$wechat->status,
                    'nickname' => $wechat->nickname,
                    'birthday' => $wechat->birthday,
                    'heading_img' =>  $wechat->heading_img,
                    'heading_img_url' =>  Config::get('config.heading_img_url'),
                ),
            );

        }while(false);
        Log::info("info", array("system" => "activity" ,"event" => "/GuangQuLu/WashLogin", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }



}